package sdk.webview.fmc.com.fmcsdk.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.CameraUtils;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.Facing;
import com.otaliastudios.cameraview.Flash;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.camera.AppConstant;
import sdk.webview.fmc.com.fmcsdk.camera.BitmapUtils;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.Utility;

public class CameraOneActivity extends BaseActivity {

    private CameraView cameraView;
    private String imagename;

    private ImageView iv_exchange;

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iv_exchange=findViewById(R.id.iv_exchange);
        imagename = getIntent().getStringExtra("imagename");
        cameraView = findViewById(R.id.cameraView);
        cameraView.setLifecycleOwner(this);
        findViewById(R.id.take).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFlash) {
                    cameraView.capturePicture();
                } else {
                    cameraView.captureSnapshot();
                }
            }
        });

        cameraView.addCameraListener(new CameraListener() {
            @Override
            public void onPictureTaken(byte[] picture) {
                // Create a bitmap or a file...
                // CameraUtils will read EXIF orientation for you, in a worker thread.
                CameraUtils.decodeBitmap(picture, new CameraUtils.BitmapCallback() {
                    @Override
                    public void onBitmapReady(Bitmap bitmap) {
                        BitmapUtils.saveJPGE_After(CameraOneActivity.this, bitmap, imagename, 100);
                        Intent intent = new Intent();
                        intent.putExtra(AppConstant.KEY.IMG_PATH, imagename);
                        setResult(AppConstant.RESULT_CODE.RESULT_OK, intent);
                        finish();
                    }
                });
            }
        });

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });

        findViewById(R.id.iv_light).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        String appName = (String) Utility.getMetaData(this, Constant.APP_NAME);
        if (Constant.APP_NAME_ZTE.equals(appName) || Constant.APP_NAME_GMY.equals(appName)) {
            ((ImageView) findViewById(R.id.iv_flash)).setVisibility(View.VISIBLE);
        }

        findViewById(R.id.iv_flash).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (cameraView.getFlash() == Flash.OFF) {
                    cameraView.setFlash(Flash.ON);
                    ((ImageView) findViewById(R.id.iv_flash)).setImageResource(R.mipmap.camera_flash_on);
                    isFlash = true;
                } else {
                    ((ImageView) findViewById(R.id.iv_flash)).setImageResource(R.mipmap.camera_flash_off);
                    cameraView.setFlash(Flash.OFF);
                    isFlash = false;
                }
            }
        });


        iv_exchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (cameraView.getFacing() == Facing.BACK) {
                    cameraView.setFacing( Facing.FRONT);
                } else {
                    cameraView.setFacing( Facing.BACK);
                }
            }
        });

    }

    boolean isFlash = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_camera_one;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraView.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        cameraView.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraView.destroy();
    }

}